/**
 * 统一API管理入口文件
 * 将各个页面的接口按模块封装，便于维护和管理
 */

import config from "./config.js";

const request = (options) => {
  return new Promise((resolve, reject) => {
    // 使用最简单的uni.request实现
    uni.request({
      url: config.baseURL + options.url,
      method: options.method || "POST",
      data: options.data || {},
      header: options.header || {},
      success: (res) => resolve(res),
      fail: (err) => reject(err),
    });
  });
};

/**
 * ==================== 用户认证相关接口 ====================
 */
const authAPI = {
  // 用户登录
  login: (data) => {
    return request({
      url: "/api/login",
      method: "POST",
      data,
    });
  },

  // 个人注册
  registerPerson: (data) => {
    return request({
      url: "/api/register/person",
      method: "POST",
      data,
    });
  },

  // 公司注册
  registerCompany: (data) => {
    return request({
      url: "/api/register/company",
      method: "POST",
      data,
    });
  },
};

/**
 * ==================== 首页相关接口 ====================
 */
const homeAPI = {
  // 获取首页数据
  getHomeData: () => {
    return request({
      url: "/api/home",
      method: "post",
    });
  },
};

/**
 * ==================== 美食相关接口 ====================
 */
const foodAPI = {
  // 获取美食页面数据
  getFoodData: () => {
    return request({
      url: "/api/food",
      method: "POST",
    });
  },

  // 根据类型获取店铺列表
  getShopsByType: (data) => {
    return request({
      url: "/api/food/shop",
      method: "POST",
      data,
    });
  },

  // 获取店铺详情
  getShopDetail: (data) => {
    return request({
      url: "/api/food/shop_detail",
      method: "POST",
      data,
    });
  },

  // 提交购物订单
  submitOrder: (data, token) => {
    console.log("submitOrder方法接收到的token:", token);
    console.log("submitOrder方法接收到的data:", data);
    return request({
      url: "/api/food/shopping_submit",
      method: "POST",
      data,
      header: {
        'Content-Type': 'application/json',
        'token': token || ''
      },
    });
  },

  // 获取商品详情
  getProductDetail: (data) => {
    return request({
      url: "/api/food/product_detail",
      method: "POST",
      data,
    });
  },
};

/**
 * ==================== 个人中心相关接口 ====================
 */
const userAPI = {
  // 获取我的收藏
  getMyCollect: (data) => {
    return request({
      url: "/api/lark/mycollect",
      method: "post",
      data,
    });
  },

  // 获取订单列表
  getOrderList: (data, token,userId) => {
    return request({
      url: "/api/order",
      method: "post",
      data,
      header: {
        Authorization: token,
        userid: userId,
      },
    });
  },
};

/**
 * ==================== 订单相关接口 ====================
 */
const orderAPI = {
  // 获取订单列表
  getOrderList: (data, token, userId) => {
    return request({
      url: "/api/order",
      method: "POST",
      data,
      header: {
        token: token,
        userid: userId,
      },
    });
  },

  // 取消订单
  cancelOrder: (orderId, data, token, userId) => {
    return request({
      url: `/api/orders/cancel`,
      method: "POST",
      header: {
        token: token,
        userid: userId,
      },
      data: {
        orderId,
        ...data,
      },
    });
  },
};

// 导出所有API模块
export { authAPI, homeAPI, foodAPI, userAPI, orderAPI };
